import { useState, useEffect, useCallback } from 'react'
import MapCenter from './MapCenter';
import BusinessCenterContext from './BusinessCenterContext';
import ModuleSelector from './ModuleSelector';
import FloodWarning from './FloodWarning';
import DetailPanel from './common/DetailPanel';
import EarthquakeWarning from './EarthquakeWarning';
import LoopMsg from './LoopMsg';
import DisasterWarning from './DisasterWarning';
import RiskDanger from './RiskDanger';
import RiskZoneAnalysis from './RiskZoneAnalysis';
import DynamicRiskAnalysis from './DynamicRiskAnalysis';
import RiskReport from './RiskReport';
import FireWarning from './FireWarning';
import Toolbar from './Toolbar';
import NewHome from '../NewHome';
import messageController from '../../../../../utils/messageController';
import { resourcePath } from '../../../../../utils/resource';
import Home from '../Home';
const items = [
    { name: '防汛监测', icon: 'fangxunjiance' },
    { name: '地震监测', icon: 'dizhenjiance' },
    { name: '地灾监测', icon: 'dizaijiance-shang-tuli' },
    { name: '火灾监测', icon: 'huozaijiance' },
    { name: '风险隐患', icon: 'fengxianyinhuan' },
    { name: '风险区划分析', icon: 'mendian' },
    { name: '动态风险分析', icon: 'dongtaifengxianfenxi' },
    { name: '风险评估', icon: 'fengxianpinggu1' },
    { name: '统计分析', icon: '07_tongjifenxi' }]
export default function BusinessCenter(props) {
    const [map, setMap] = useState();
    const [module, setModule] = useState('统计分析');

    const onLoaded = useCallback((m) => {
        setMap(m)
    }, [])

    useEffect(() => {
        const handler = () => {
            setModule('防汛监测')
        }
        messageController.addListener('to-map', handler)
        return () => {
            messageController.removeListener('to-map', handler);
        }
    }, [])

    useEffect(() => {
        const handler = (path) => {
            setModule(path)
        }
        messageController.addListener('to-path', handler)
        return () => {
            messageController.removeListener('to-path', handler);
        }
    }, [])

    useEffect(() => {
        if (module === '统计分析') {
            setMap(null);
        }
    }, [module])

    return <BusinessCenterContext.Provider value={{
        map,
        module
    }}>
        {
             module === '统计分析' ? <Home /> :
            <>
                <MapCenter onLoaded={onLoaded}></MapCenter>

                <div style={{
                    // background: `url(${resourcePath.mapBg})`,
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    right: 0,
                    bottom: 0,
                    backgroundSize: "100% 100%",
                    pointerEvents: "none"
                }}>
                </div>
                {map && <>
                    {/* <ModuleSelector items={items} value={module} onChange={v => {
                        setModule(v)
                    }} /> */}
                    {/* {
                        module === '统计分析' && <Home />
                    } */}
                    {
                        module === '防汛监测' && <FloodWarning />
                    }
                    {
                        module === '地震监测' && <EarthquakeWarning />
                    }
                    {
                        module === '地灾监测' && <DisasterWarning />
                    }
                    {
                        module === '风险隐患' && <RiskDanger />
                    }
                    {
                        module === '风险区划分析' && <RiskZoneAnalysis />
                    }
                    {
                        module === '动态风险分析' && <DynamicRiskAnalysis />
                    }
                    {
                        module === '风险评估' && <RiskReport />
                    }
                    {
                        module === '火灾监测' && <FireWarning />
                    }
                    <Toolbar />
                    <DetailPanel />
                    <LoopMsg />
                </>}
            </>
        }

    </BusinessCenterContext.Provider>;
}